package services;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.tools.ant.taskdefs.email.EmailAddress;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.contacts.UserDefinedField;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import datastore.Contact;

public class TagParticipantService {

	private static final String DEFAULT_FEED = "http://www.google.com/m8/feeds/contacts/aliceprivacy@gmail.com/full";
//	private static final DBService dbService = new DBService();
	
	private ContactsService contactsService = new ContactsService("exampleCo-exampleApp-2");
	private URL feedUrl = null;
	
	public TagParticipantService(){
//		try {
//			contactsService.setUserCredentials("aliceprivacy@gmail.com", "1Alice2Privacy3");
//			
////			URL feedUrl = new URL(DEFAULT_FEED);
////			resultFeedGlobal = contactsService.getFeed(feedUrl, ContactFeed.class);
//		} catch (AuthenticationException e) {
//			// TODO Auto-generated catch block
//			e.getStackTrace();
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ServiceException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

	}

	
//	public String getPassword(String name) throws AuthenticationException{
//		Contact contact = dbService.getContact(name);
//		return contact.getPassword();
//		
//	}

//	private ContactsService getContactsService(String email){
//		
//		ContactsService myService = new ContactsService("exampleCo-exampleApp-4");
//		try {
//			myService.setUserCredentials(email, getPassword(email));
//		} catch (AuthenticationException e) {
//			// TODO Auto-generated catch block
//			e.getStackTrace();
//		}
//		
//		return myService;
//	}
	
	private ContactFeed getAliceContact(){
		
		List<ContactEntry> contactEntries = new ArrayList<ContactEntry>();
		
		
		ContactEntry contactEntry = new ContactEntry();
		Email email = new Email();
		email.setAddress("carolprivacy@gmail.com");
		contactEntry.addEmailAddress(email);
		
		
//		UserDefinedField userDefinedField = new UserDefinedField("actor:Dana","actor:Dana");
//		contactEntry.addUserDefinedField(userDefinedField);
		UserDefinedField userDefinedField = new UserDefinedField("person:Brad","person:Brad");
		contactEntry.addUserDefinedField(userDefinedField);
		userDefinedField = new UserDefinedField("location:Brad's_home","location:Brad's_home");
		contactEntry.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("actor:Alice","actor:Alice");
//		contactEntry.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("location:gym","location:gym");
//		contactEntry.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("time:7pm","time:7pm");
//		contactEntry.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("aboutPerson:Carol ","aboutPerson:Carol ");
//		contactEntry.addUserDefinedField(userDefinedField);
		
		ContactEntry contactEntry1 = new ContactEntry();
		Email email1 = new Email();
		email1.setAddress("danaprivacy@gmail.com");
		contactEntry1.addEmailAddress(email);
		
//		userDefinedField = new UserDefinedField("actor:Dana","actor:Dana");
//		contactEntry1.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("actor:Brad","actor:Brad");
//		contactEntry1.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("actor:Alice","actor:Alice");
//		contactEntry1.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("aboutPerson:Carol ","aboutPerson:Carol ");
//		contactEntry1.addUserDefinedField(userDefinedField);
//		userDefinedField = new UserDefinedField("contentType:photo ","contentType:photo");
//		contactEntry1.addUserDefinedField(userDefinedField);
		userDefinedField = new UserDefinedField("activity:drinking","activity:drinking");
		contactEntry1.addUserDefinedField(userDefinedField);		
		
		contactEntries.add(contactEntry);
		contactEntries.add(contactEntry1);
		
		
		ContactFeed contactFeed = new ContactFeed();
		contactFeed.setEntries(contactEntries);
		
		return contactFeed;
	}
	
	private ContactEntry findUserByEmail(String emailParticipant) throws IOException, ServiceException{
		ContactEntry entry = null;
		
//		if(feedUrl == null){
//			feedUrl = new URL(DEFAULT_FEED);
//			contactsService.setUserCredentials("aliceprivacy@gmail.com", "1Alice2Privacy3");
//		}
//		ContactFeed resultFeed = contactsService.getFeed(feedUrl, ContactFeed.class);
		ContactFeed resultFeed = getAliceContact();
		
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
			 entry = resultFeed.getEntries().get(i);
			 
			 for (Email email : entry.getEmailAddresses()) {
				 if(email.getAddress().equals(emailParticipant))
					 return entry;
			 }
			 
		}
		
		return entry;
	}
	
	public boolean isContentOfBlipValidWithParticipants(String tagInString, String emailsParticipants,String emailInput) throws IOException, ServiceException{

		// URL feedUrl = new URL(DEFAULT_FEED.replace("***", emailInput));
//		URL feedUrl = new URL(DEFAULT_FEED);
//		ContactFeed resultFeed;
//		if(resultFeedGlobal == null){
//			URL feedUrl = new URL(DEFAULT_FEED);
//			resultFeed = contactsService.getFeed(feedUrl, ContactFeed.class);
//			resultFeedGlobal = resultFeed;
//		}else{
//			resultFeed = resultFeedGlobal;
//		}
		
		
		String [] emails = emailsParticipants.split(" ");
		String [] tags = tagInString.split(" ");
		ContactEntry entry;		
		System.out.println("44");
		
		for(String email: emails){
			email = email.replace("googlewave.com", "gmail.com");
			if((entry = findUserByEmail(email)) != null){
				System.out.println(email + "-----");
				System.out.println(entry.getUserDefinedFields().size() + "-----");
				if(isTagInUserUserDefinedFields(tags,entry.getUserDefinedFields())){
					System.out.println("----true----");
					return true;
				}
			}
		}
		
		System.out.println("----false----");
		
		return false;
	}
	
	private boolean isTagInUserUserDefinedFields(String [] tags, List<UserDefinedField> userDefinedFields){
		for(UserDefinedField userDefinedField: userDefinedFields){
			String userDefinedFieldString = userDefinedField.getValue();
			for(String tag: tags){
//				String tagString = tag;
				System.out.println(tag.indexOf(userDefinedFieldString) != -1);
//				
//				System.out.println(userDefinedFieldString.indexOf(tagString) != -1);
				
				if(userDefinedFieldString.equals(tag)){
					return true;
				}
			}
		}
		return false;
	}
	
	public String hasAddParticipant(String emailInput,String emailParticipant, Map<String,String> mapWavelet) throws IOException, ServiceException{
		
//		ContactsService contactsService = new ContactsService("exampleCo-exampleApp-4");
//		try {
//			contactsService.setUserCredentials(emailInput, getPassword(emailInput));
//		} catch (AuthenticationException e) {
//			// TODO Auto-generated catch block
//			e.getStackTrace();
//		}
		
		// URL feedUrl = new URL(DEFAULT_FEED.replace("***", emailInput));
//		ContactFeed resultFeed = null;
//		if(resultFeedGlobal == null){
//			URL feedUrl = new URL(DEFAULT_FEED);
//			resultFeed = contactsService.getFeed(feedUrl, ContactFeed.class);
//			resultFeedGlobal = resultFeed;
//		}else{
//			resultFeed = resultFeedGlobal;
//		}
		
		
			String wrongTagsInWavelet = "";
			
		
			Set<Entry<String, String>> entries = mapWavelet.entrySet();
			 
			
			 
			for(UserDefinedField userDefinedField: findUserByEmail(emailParticipant).getUserDefinedFields()){
				// System.out.println("USER =>" + userDefinedField.getValue());
				for(Entry entryTmp: entries){
					System.out.println("----" + entryTmp.getValue() +"--"+ userDefinedField.getValue());
					if(((String)entryTmp.getValue()).indexOf(userDefinedField.getValue()) != -1){
						
						if(wrongTagsInWavelet.indexOf(userDefinedField.getValue()) == -1){
							wrongTagsInWavelet += userDefinedField.getValue() + " ";
						}
					} 
				}
			}
	
		
			
		return wrongTagsInWavelet;
	}
}
